using UnityEngine;
using QFramework;
using System;
using System.Collections;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class AK : Gun
	{
        public override AudioSource AudioSource => SelfAudioSource;

        public override float AtkTime => atkTime;

        private float atkTime = 0;

        private Vector2 dics;
        ShootDuration shootDuration;

        private void Awake()
        {
            RemoveBulletCount();
        }

        public override void GetDic(Vector2 dic)
        {
            base.GetDic(dic);
            dics = dic;
        }
        public override void Shoot(Vector2 dic)
        {
            base.Shoot(dic);
            DataManager.Instance.aKModel.CanFireIndex.Value -= 1;
            var playerBullet = Instantiate(AkBullet);
            playerBullet.transform.position = AkBullet.transform.position;
            playerBullet.SetBulletSpeed(DataManager.Instance.aKModel.AKBulletSpeed.Value);
            playerBullet.SetBulletScale(DataManager.Instance.aKModel.AKBulletScale.Value);
            playerBullet.BulletMoveDic(dic);
            playerBullet.Show();
        }
        public override void ShootDown(Vector2 dic)
        {
            base.ShootDown(dic);
            if (DataManager.Instance.aKModel.CanFireIndex.Value > 0)
            {
                Shoot(dic);
                AudioSource.clip = ShootSounds[0];
                AudioSource.loop = true;
                AudioSource.Play();
            }
           
        }

        public override void Shooting(Vector2 dic)
        {
            base.Shooting(dic);
            if (atkTime > DataManager.Instance.mpFiveModel.MpFiveAtkSpeed.Value&&DataManager.Instance.aKModel.CanFireIndex.Value>0)
            {
                atkTime = 0;
                Shoot(dic);
            }
            atkTime += Time.deltaTime;
        }
        public override void ShootUp(Vector2 dic)
        {
            base.ShootUp(dic);
            AudioSource.Stop();
        }
       

        void Start()
		{
            shootDuration = new ShootDuration().
                WithDuration(DataManager.Instance.aKModel.AKAtkSpeed.Value);
            SetInputAction(() => Shooting(dics), 
                () => ShootDown(dics), () => ShootUp(dics));
        }

        private void Update()
        {
            MouseDown();
        }



        #region 换弹逻辑


        protected override IEnumerator RemoveBullet(float time)
        {
            return base.RemoveBullet(time);
        }

        public override void RemoveBulletCount()
        {
            base.RemoveBulletCount();
            DataManager.Instance.aKModel.CanFireIndex.Register(value =>
            {
                if (value <= 0)
                {
                    this.Delay(0.25f, () => StartCoroutine(RemoveBullet(DataManager.
                        Instance.aKModel.AKRemoveSpeed.Value)));
                }

            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            
        }

        public override void OnReloadStart()
        {
            base.OnReloadStart();
            Guns.Hide();
            RemoveAnim.Show();
            AudioSource.Stop();
        }

        public override void OnReloadFinished()
        {
            base.OnReloadFinished();
            RemoveAnim.Hide();
            Guns.Show();
            DataManager.Instance.aKModel.CanFireIndex.Value =
                DataManager.Instance.aKModel.AKGunBulletCount.Value;
        }
        #endregion
    }
}
